<?php
$database = "db";
$Tables_in = "Tables_in_".$database;

function writeFile($data){
        $fpt = fopen('schema.xml','a');
        fwrite($fpt,$data);
        fwrite($fpt,"\n");
}
writeFile('<?xml version="1.0" encoding="UTF-8"?>');
writeFile(	'<database name="'.$database.'" defaultIdMethod="native" noxsd="true">');

mysql_connect("localhost","root","");
@mysql_select_db($database) or die( "Unable to select database");

$result = mysql_query("show tables") or die(mysql_error());
while($obj = mysql_fetch_object($result)){
	writeFile('	<table name="'.$obj->$Tables_in.'">');	
	$tables = mysql_query("describe ".$obj->$Tables_in) or die(mysql_error());
	while($describe = mysql_fetch_object($tables)){
		$Type = explode('(',$describe->Type);
		//echo '<br />'.$Type[0];
		//print_r($describe);				
		$size = substr($Type[1], 0, strlen($Type[1])-1);		
		
		if($Type[0]=='int'){
			$Type[0] = 'integer';		
		}else if($Type[0]=='text'){
			$Type[0] = 'longvarchar';
		}
		if($describe->Key!=''){
			if($describe->Key=='PRI'){
				if($size == ''){
					writeFile('		<column name="'.$describe->Field.'" type="'.$Type[0].'" primaryKey="true" />');
				}else{
					writeFile('		<column name="'.$describe->Field.'" type="'.$Type[0].'" size="'.$size.'" primaryKey="true" />');
				}
			}
		}else{
			if($size == ''){
				writeFile('		<column name="'.$describe->Field.'" type="'.$Type[0].'" />');
			}else{
				writeFile('		<column name="'.$describe->Field.'" type="'.$Type[0].'" size="'.$size.'" />');
			}
		}	
	}
	writeFile('	</table>');
}
writeFile('</database>');
?>